package com.javasm.finance.service.impl;

import com.javasm.finance.dao.UserDao;
import com.javasm.finance.dao.impl.UserDaoImpl;
import com.javasm.finance.entity.User;
import com.javasm.finance.entity.UserMenu;
import com.javasm.finance.service.UserService;
import com.javasm.finance.util.DBUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();

    @Override
    public User findLoginUser(String userName, String userPwd) {
        User user=null;
        try {
            user=userDao.findLoginUser(userName,userPwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public List<String> findLoginUserMenuUrl(Integer userId) {
        List<String> loginUserMenuUrl = null;
        try {
            loginUserMenuUrl = userDao.findLoginUserMenuUrl(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return loginUserMenuUrl;
    }

    @Override
    public List<User> findUser(Integer page, Integer pageSize, User user) {
        List<User> userList = null;
        try {
            userList = userDao.findUser(page, pageSize, user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }

    @Override
    public long findTotalNum(User user) {
        long totalNum = 0;
        try {
            totalNum = userDao.findTotalNum(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalNum;
    }

    @Override
    public int addUser(User user) {
        int rows = 0;
        try {
            rows = userDao.addUser(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public User findByUid(Integer userId) {
        User user = null;
        try {
            user = userDao.findByUid(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public int editUser(User user) {
        int rows = 0;
        try {
            rows = userDao.editUser(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }

    @Override
    public boolean deleteUser(Integer userId) {
        boolean isSuccess = false;
        Connection conn = DBUtils.getConn();
        try {
            //关闭事务自动提交
            conn.setAutoCommit(false);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            //删除用户数据
            userDao.deleteUser(userId, conn);
            //若存在用户权限数据，则直接删除
            List<UserMenu> userMenuList = userDao.findUserMenu(userId);
            if (userMenuList != null && userMenuList.size() > 0) {
                userDao.deleteUserMenu(userId, conn);
            }
            //提交事务
            DbUtils.commitAndCloseQuietly(conn);
            isSuccess = true;
        } catch (SQLException throwables) {
            //throwables.printStackTrace();
            DbUtils.rollbackAndCloseQuietly(conn);
        }
        return isSuccess;
    }

    @Override
    public int editLoginTime(Integer userId) {
        int rows = 0;
        try {
            rows = userDao.editLoginTime(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }
}
